package com.hkzhao.shixun.config;

import com.hkzhao.shixun.dto.QueueEnum;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.management.Query;

/**
 * <h3>j2ee</h3>
 * <p>mq注册队列</p>
 *
 * @author : hkzhao
 * @date : 2020-06-05 12:43
 **/
@Configuration
public class RabbitMqConfig {
    //超时没填写中奖信息自动取消的绑定交换机
    @Bean
    DirectExchange cancleLotteryDirect(){
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_ENUM_CancleLottery.getExchange())
                .durable(true)
                .build();
    }

    //超时没填写中奖信息自动取消的死信队列交换机
    @Bean
    DirectExchange cancleLotteryTtlDirect(){
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_ENUM_CancleLottery_TTL.getExchange())
                .durable(true)
                .build();
    }

    //注册取消中奖消息队列

    @Bean
    public Queue  cancleLotteryQueue(){
        return new Queue(QueueEnum.QUEUE_ENUM_CancleLottery.getName());
    }

    //注册取消中奖消息的延迟队列 即死信

    @Bean
    public Queue cancleLotteryTtlQueue(){
        return QueueBuilder.durable(QueueEnum.QUEUE_ENUM_CancleLottery_TTL.getName())
                .withArgument("x-dead-letter-exchange",QueueEnum.QUEUE_ENUM_CancleLottery.getExchange())
                //到期转发的交换机名字
                .withArgument("x-dead-letter-routing-key",QueueEnum.QUEUE_ENUM_CancleLottery.getRouteKey())
                //到期转发的路由键
                .build();
    }

    //队列绑定到交换机上

    @Bean
    Binding cancleLotteryTtlBinding(DirectExchange cancleLotteryTtlDirect,Queue cancleLotteryTtlQueue){
        return BindingBuilder.bind(cancleLotteryTtlQueue)
                .to(cancleLotteryTtlDirect)
                .with(QueueEnum.QUEUE_ENUM_CancleLottery_TTL.getRouteKey());
    }
    @Bean
    Binding cancleLotteryBinding(DirectExchange cancleLotteryDirect,Queue cancleLotteryQueue){
        return BindingBuilder.bind(cancleLotteryQueue)
                .to(cancleLotteryDirect)
                .with(QueueEnum.QUEUE_ENUM_CancleLottery.getRouteKey());
    }
}
